var express = require('express');
var router = express.Router();



/* GET users listing. */
var user = require('../service/user');
console.log(typeof  user.list);
router.use(function(req,res,next){
    //console.log(new Date());
    //console.log(req.originalUrl);
    next();
});

router.get('/', function(req, res, next) {
//    function outJson(data){
//        res.json(data);
//    }
//    user.list(0,0,outJson);
    res.send("/user/ is invoke");
});
router.all('/list',function(req,res,next){
   console.log('router all is invoke');
//
    user.list(req.query.page,req.query.rows,function(data){
        res.json(data);
    });
//    next();
});
router.post('/edit',function(req,res,next){
    console.log(req.body);
    user.edit({
        id:req.body.id,
        name:req.body.name
    },
    function(data){
        res.json(data);
    })
});
router.post('/add',function(req,res,next){
    user.add({
        id:null,
        name:req.body.name,
        departmentId:null
    },
    function(data){
        res.json(data);
    })
});
/**
 * delete处理
 * request:id：['6','5']
 * return:{affectedRows:result.affectedRows}
 */
router.post('/delete',function(req,res,next){
    //console.log(req.body.id);
    //console.log(typeof  req.body.id);
    var ids = [];
    if(typeof req.body.id == 'string'){
        ids.push(req.body.id);
    }
    else{
        ids =  req.body.id;
    }
    //console.log(id);
    for(var i in ids){
        //console.log(i);
        ids[i] = parseInt(ids[i]);
        //console.log(id[i]);
    }
    console.log(ids);
    user.delete(ids,
        function(data){
            res.json(data);
        });
})
router.get('/list',function(req,res,next){
    console.log(req.query.page);
    //console.log(req.param('page'));
//    res.send(req.query);
    user.list(req.query.page,req.query.rows,function(data){
        res.json(data);
    })
});
router.get('/add',function(req,res,next){

});
router.get('/edit',function(req,res,next){

});
router.get('/delete',function(req,res,next){

});
module.exports = router;
